var express=require('express');
var router = express.Router();
const Result = require('../models/Result')

// 上传图片到阿里云oss使用
var fs = require('fs');
// 初始化Client
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
  region: 'oss-cn-chengdu',//填写你开通的oss
  accessKeyId: 'LTAI4GBq2fr31Wu3xHNgTTVr',
  accessKeySecret: 'gdi2gSd8TwLrtkZppO2VVjcXU6xi9O'
});

var ali_oss = {
    bucket: 'huijiaoe',  //阿里云您的bucket
    endPoint: 'oss-cn-chengdu.aliyuncs.com', //填写你开通的oss地址
}

 //图片上传
var multer  = require('multer')
var upload = multer({ dest: './tmp/' })
router.post('/upload',upload.single('file'),function(req, res, next){
		//  文件路径
		var filePath = './' + req.file.path; 
		// 文件类型
		var temp = req.file.originalname.split('.');
		var fileType = temp[temp.length - 1];
		var lastName = '.' + fileType;
		// 构建图片名
		var fileName = Date.now() + lastName;
		// 图片重命名
		fs.rename(filePath, fileName, (err) => {
			if (err) {
				res.json(JSON.stringify({status:'102',msg:'文件写入失败'}));   
			}else{

				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var day = date.getDate();
				if (month < 10) {
					month = "0" + month;
				}
				if (day < 10) {
					day = "0" + day;
				}
				var nowDate = year  + month  + day;
				
				var localFile = './' + fileName;  
				var key = 'images/'+nowDate+'/'+fileName;
				// 阿里云 上传文件 
				co(function* () {
					client.useBucket(ali_oss.bucket);
					var result = yield client.put(key, localFile);
					//自定义使用域名访问图片，（别忘记把域名解析至oss）
					var imageSrc = 'https://huijiaoe.oss-cn-chengdu.aliyuncs.com/' + result.name;
					// 上传之后删除本地文件
					fs.unlinkSync(localFile);
					new Result({
						imageUrl:imageSrc
					},'上传成功').success(res)
				}).catch(function (err) {
					// 上传之后删除本地文件
					fs.unlinkSync(localFile);
					new Result('上传失败').fail(res)
				});
			}
		});
	})
module.exports = router;   //暴露这个 router模块